// pages/user_end/article/article.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
const WxParse = require('../../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    info:[],
	login: 0,
	userinfo: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
	 
     this.setData({
       id:options.id
     })	
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
	var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        console.log('etime'+etime);
		console.log(etime < res.expiretime);
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
        
        that.getQuestion();	
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  goLogin: function () {
    console.log('登录');
	if (this.data.login == 0) {
	  wx.navigateTo({
	    url: '../../authorization/authorization'
	  })
	  return false;
	}
   
  },
  gotopeixun(e){
  
  var id =e.currentTarget.dataset.id;
  var money =e.currentTarget.dataset.money;
  wx.navigateTo({
    url:"../peixun/peixun?id="+id+'&money='+money
  })
  },
  getQuestion:function(){
	  console.log('login'+this.data.login);
	  if (this.data.login == 0) {
	    this.goLogin();
	    return false;
	  }
    var that = this;
    let param = {
      scoure: 'wxapp',
      id: that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getpxdetail, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
        that.setData({
          info:res.data.data
        })
        if(!res.data.data){
         wx.showToast({
           title:'该文章不存在!',
           icon:'none'
         });			
         setTimeout(function(){
           wx.navigateBack({
             delta:1
           })
         },1500)							 
        }
        wx.setNavigationBarTitle({
          title: res.data.data.title,
        });
        let content = res.data.data.brief_content;
        WxParse.wxParse('articlecontent', 'html', content, that, 0)
      }
    })  							
  }
})